<?php
class M_barang extends CI_Model{
	
		function getlist($limit,$offset,$filter){
			$this->db->limit($limit);
			$this->db->offset($offset);
			$this->db->select("barang_id,barang_kode,barang_nama, barang_merk, barang_jenis, IF(barang_jenis = 1, 'Habis Pakai','Aset') as barang_diplay ,barang_active, m_satuan_id , s.satuan_nama",false);
			$this->db->from('m_barang as b');
			$this->db->join('m_satuan as s', 's.satuan_id = b.m_satuan_id', 'left');
			$this->db->where('barang_active','y');
			if(!empty($filter)){
				$this->db->where("lower(`barang_nama`)  like '%".addslashes(strtolower($filter))."%'"); 
			}
			$query = $this->db->get();
			$total = $this->db->select('*')->from('m_barang')->where('barang_active','Y')->count_all_results();
			$arr   = array();
			foreach($query->result() as $q){
				$arr[] = $q;
			}
			$final = array('results' => $arr, 'total' => $total);
			return json_encode($final);
		}
	
		
		function getlistout($limit,$offset,$filter){
			$this->db->limit($limit);
			$this->db->offset($offset);
			$this->db->select("m_barang_id,barang_kode,barang_nama, barang_merk, barang_jenis, IF(barang_jenis = 1, 'Habis Pakai','Aset') as barang_diplay ,barang_active, m_satuan_id , s.satuan_nama,tbm.barangmasuk_id,",false);
			$this->db->from('m_barang as b');
			$this->db->join('m_satuan as s', 's.satuan_id = b.m_satuan_id', 'left');
			$this->db->join('t_barangmasukdet as tbd', 'tbd.m_barang_id = b.barang_id', 'left');
			$this->db->join('t_barangmasuk as tbm', 'tbm.barangmasuk_id = tbd.t_barangmasuk_id', 'left');
			$this->db->where('barang_active','y');
			$this->db->where('tbm.barangmasuk_id is not null');
			if(!empty($filter)){
				$this->db->where("lower(`barang_nama`)  like '%".addslashes(strtolower($filter))."%'"); 
			}
			$query = $this->db->get();
			$total = $this->db->select("m_barang_id,barang_kode,barang_nama, barang_merk, barang_jenis, IF(barang_jenis = 1, 'Habis Pakai','Aset') as barang_diplay ,barang_active, m_satuan_id , s.satuan_nama",false)->from('m_barang as b')->join('m_satuan as s', 's.satuan_id = b.m_satuan_id', 'left')->join('t_barangmasukdet as tb', 'tb.m_barang_id = b.barang_id', 'left')->where('barang_active','Y')->where('m_barang_id is not null')->count_all_results();
			$arr   = array();
			foreach($query->result() as $q){
				$arr[] = $q;
			}
			$final = array('results' => $arr, 'total' => $total);
			return json_encode($final);
		}
		
		function insert($data){
            $this->db->insert('m_barang',$data);
        }
		
		function update($data){
			extract($data);
			$data_submit = array(
				'barang_id'		 => $this->input->post('barang_id'),
				'barang_kode'    => $this->input->post('barang_kode'),
				'barang_nama'	 => $this->input->post('barang_nama'),
				'barang_merk'    => $this->input->post('barang_merk'),
				'barang_jenis'   => $this->input->post('barang_jenis'),
				'm_satuan_id'	 => $this->input->post('barang_satuan')
			);
			$this->db->where('barang_id',$barang_id);
			$this->db->update('m_barang',$data_submit);
		}
		
		function delete($data){
			extract($data);
			$data_baru = array(
				'barang_id'		 => $this->input->post('barang_id'),
				'barang_kode'    => $this->input->post('barang_kode'),
				'barang_nama'	 => $this->input->post('barang_nama'),
				'barang_merk'    => $this->input->post('barang_merk'),
				'barang_jenis'   => $this->input->post('barang_jenis'),
				'barang_active'  =>'n'
			);
			$this->db->where('barang_id',$barang_id);
			$this->db->update('m_barang',$data_baru);
		}
	
	

}